<?php
 
session_start();
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/config/mblog_config.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/core/mblog/weibooauth.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/love/inc.php");
error_reporting(E_ERROR | E_PARSE);

//error_reporting(E_ALL);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	cgi::both($g_cgival["id"], "id", "");
	$g_cgival["id"] = intval($g_cgival["id"]);

	if(!$g_cgival["id"]) throw new Exception("迷路了？");
	
	cgi::both($g_cgival["p"], "p", 1);
	
	cgi::both($g_cgival["pcount"], "pcount", 10);
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if(!$_SESSION['last_key']['oauth_token'])
	{
		header("location:/sina_api/?url=/love/bless.php?id={$g_cgival["id"]}");
		exit;
	}
	
	$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
	$current_user_info  = $c->verify_credentials(); // done
	$current_uid = $current_user_info["id"];
	
	$id = $g_cgival["id"] ;
	$db_love_request = new db_love_request();
	$request_info = $db_love_request->get_item_by_id($id);
	
	$db_love_bless  = new db_love_bless();
	$bless_count = $db_love_bless->get_count_by_request_id($id);
	$bless_list = $db_love_bless->get_list_by_request_id($id);
	
	
	$g_show["request_info"] = $request_info;
	$g_show["bless_list"] = $bless_list;
	$g_show["bless_count"] = $bless_count;
	
}
try {
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";
	
	
} 
catch (Exception $e) 
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
	
}
show_pro();

exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	
	$smarty = p_get_smarty();	
	$smarty->assign("g_show", $g_show);

	if($g_show["error"] ==1)
	{
		echo $g_show["errmsg"];
	}
	else
	{
		$smarty->display("love/bless.html");
		
	}
	echo "<pre>";
	print_r($g_show);

	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
}



?>
